package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.zhaoshang.ZsProject;
import com.yanqu.road.entity.zhaoshang.ZsProjectAdditionData;
import com.yanqu.road.entity.zhaoshang.result.ZsProjectOpenResult;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.assistant.CrossAssistantMgr;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.zhaoshang.UserLockerMgr;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C_ZS_ASSISTANT_ACTION, desc = "招商小助手处理")
public class CrossZsAssistantActionCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        AssistantProto.AssistantCrossActionReqMsg assistantCrossActionReqMsg = AssistantProto.AssistantCrossActionReqMsg.parseFrom(packet.getBytes());
        AssistantProto.CrossAssistantZsProjectOpenReqMsg reqMsg = assistantCrossActionReqMsg.getZsReqMsg();
        UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());

        AssistantProto.AssistantActionRespMsg.Builder actionRespBuilder = assistantCrossActionReqMsg.getAssistantActionRespMsg().toBuilder();

        for (ZhaoShangProto.ZsProjectOpenReqMsg openReqMsg : reqMsg.getOpenInfoList()) {
            long playerId = packet.getPlayerId();
            ZsProject myProject = new ZsProject();
            myProject.setAddition(reqMsg.getAddition());
            myProject.setTemplateId(openReqMsg.getTemplateId());
            myProject.setTitleAddition(reqMsg.getTitleAddition());
            myProject.setDecorationOverTime(reqMsg.getDecorationOverTime());

            ZsProjectAdditionData additionData = new ZsProjectAdditionData();
            additionData.setInsertOption();
            additionData.setCurioAddition(reqMsg.getSkillAddition());
            myProject.setAdditionData(additionData);


            ZsProjectOpenResult result = new ZsProjectOpenResult();
            result.setOpenNumLimit(reqMsg.getOpenNumLimit());

            int onceRet = 0;
            int hours = DateHelper.getHours();
            if(hours >= reqMsg.getTimeLimitList().get(0) && hours < reqMsg.getTimeLimitList().get(1)) {
                synchronized (UserLockerMgr.getUserLocker(playerId)) {
                    onceRet = ZsMgr.openProject(playerId, serverId, openReqMsg.getNum(), userBaseInfo, openReqMsg.getTemplateId(), myProject, result);
                }
            }else {
                onceRet = GameErrorCode.E_ZS_PROJECT_NO_IN_OPEN_TIME;
            }

            if (onceRet == 0) {
                ZsMgr.sendPlayerProjectStateInfo(serverId, playerId);
                if(result.getNotice() != null){
                    ZsMgr.addProjectNotice(myProject.getProjectId(), result.getNotice());
                }
            }

            ZhaoShangProto.ZsProjectOpenRespMsg.Builder builder = ZhaoShangProto.ZsProjectOpenRespMsg.newBuilder();
            builder.setRet(onceRet);
            builder.setTemplateId(openReqMsg.getTemplateId());
            builder.setProjectId(myProject.getProjectId());
            builder.setNum(openReqMsg.getNum());
            actionRespBuilder.addZsProjectOpenRespMsg(builder);
        }

        AssistantProto.AssistantCrossActionRespMsg.Builder builder = AssistantProto.AssistantCrossActionRespMsg.newBuilder();
        builder.setAssistantActionReqMsg(assistantCrossActionReqMsg.getAssistantActionReqMsg());
        builder.setAssistantActionRespMsg(actionRespBuilder);
        builder.setPreCost(reqMsg.getPreCost());

        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_ASSISTANT_ZS_ACTION_FROM_CROSS, builder);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {
    }
}
